Page({

  /**
   * 页面的初始数据
   */
  data: {
    buttonClicked: false,
    listArr: [],
    cainame: '',
    hotSearchList: [
      "小鸡炖蘑菇",
      "西瓜",
      "番茄炒鸡蛋",
      "糯米鸡",
      "哈密瓜",
      "黄焖鸡",
      "辣椒炒肉",
      "红烧肉",
    ],
    hisSearch: [
      '1'
    ],
    keyword: ''
  },
  delItem: function (e) {
    var arr = this.data.hisSearch
    arr.splice(e.currentTarget.dataset.item, 1)
    console.log(arr);
    this.setData({
      hisSearch: arr
    })

  },
  deleteAll: function (e) {
    wx.showModal({
      title: '确定要删除所有记录吗?',
      success: () => {
        this.setData({
          hisSearch: []
        })
        wx.setStorageSync('hisSearch', [])
      },
    })
  },
  submit: function (e) {
    if (e.detail.value == "")
      return
    var arr = this.data.hisSearch
    arr.unshift(e.detail.value)
    arr = arr.filter(function (element, index, self) {
      return self.indexOf(element) === index;
    });
    console.log(arr);
    this.setData({
      hisSearch: arr
    })
  },
  buttonClicked: true,
  goToSeach: function (data) {
    var that = this
    if (data == '') {
      return
    } 
    that.setData({
      keyword:data
    })
      wx.request({
        url: 'https://www.daizhongdai.xyz:3007/like/query?name=' + data,
        data: {
          // search: e.detail.value
        },
        success: res => {
          var resObj = res.data.data;
          console.log(resObj)
          that.setData({
            listArr: resObj.data
          })
         
        }
      })
   
  },
  search: function (e) {
    //防止短时间内search多次 ?
    // that.setData({
    //   buttonClicked: true
    // })
    // setTimeout(function () {
    //   that.setData({
    //     buttonClicked: false
    //   })
    // }, 1000)

    if (e.currentTarget.dataset.item !== "") {
     this.goToSeach(e.currentTarget.dataset.item)
    }
   else  if (e.detail.value !== '') {
     this.goToSeach(e.detail.value)
    }else {
      this.setData({
        listArr:[]
      })
    }
  },
  back: function () {
    wx.navigateBack()
  },
  dispath: function (e) {
    //e.currentTarget.dataset.text获取点击的模块的值
    console.log(e);
    wx.navigateTo({
      url: '../dishDetails/dishDetails?info=' + encodeURIComponent(JSON.stringify(e.currentTarget.dataset.index)),
    })
    console.log(e.currentTarget.dataset.index);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // var cainame=options.name;
   if(options.name){
     this.goToSeach(options.name)
   }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      hisSearch: wx.getStorageSync('hisSearch') == "" ? [] : wx.getStorageSync('hisSearch')
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log('存');
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

    wx.setStorageSync('hisSearch', this.data.hisSearch)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})